﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

namespace WTSQCB
{
    public partial class HocKy : System.Web.UI.Page
    {        
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        CultureInfo viVN = new CultureInfo("vi-VN");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                NgayBD_Load();
                NgayKT_Load();
            }                
        }

        protected void Thembtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HocKiThem";
                cmd.Parameters.AddWithValue("@mahocki", MaHocKitxb.Text);
                cmd.Parameters.AddWithValue("@tenhocki", TenHocKitxb.Text);
                cmd.Parameters.AddWithValue("@tennamhoc", TenNamHoctxb.Text);
                cmd.Parameters.AddWithValue("@tungay", DateTime.Parse(NamBDddl.SelectedValue + "/" + ThangBDddl.SelectedValue + "/" + NgayBDddl.SelectedValue));
                cmd.Parameters.AddWithValue("@denngay", DateTime.Parse(NamKTddl.SelectedValue + "/" + ThangKTddl.SelectedValue + "/" + NgayKTddl.SelectedValue));  
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HocKidgv.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Suabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HocKiSua";
                cmd.Parameters.AddWithValue("@mahocki", MaHocKitxb.Text);
                cmd.Parameters.AddWithValue("@tenhocki", TenHocKitxb.Text);
                cmd.Parameters.AddWithValue("@tennamhoc", TenNamHoctxb.Text);
                cmd.Parameters.AddWithValue("@tungay", DateTime.Parse(NamBDddl.SelectedValue + "/" + ThangBDddl.SelectedValue + "/" + NgayBDddl.SelectedValue));
                cmd.Parameters.AddWithValue("@denngay", DateTime.Parse(NamKTddl.SelectedValue + "/" + ThangKTddl.SelectedValue + "/" + NgayKTddl.SelectedValue));
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HocKidgv.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void Xoabtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HocKiXoa";
                cmd.Parameters.AddWithValue("@mahocki", MaHocKitxb.Text);                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HocKidgv.DataBind();
                Response.Write("Xóa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void HocKidgv_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = HocKidgv.SelectedRow;
            MaHocKitxb.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenHocKitxb.Text= HttpUtility.HtmlDecode(dong.Cells[2].Text);
            TenNamHoctxb.Text = HttpUtility.HtmlDecode(dong.Cells[3].Text);
            NgayBDddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Day.ToString();
            ThangBDddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Month.ToString();
            NamBDddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[4].Text)).Year.ToString();
            NgayKTddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[5].Text)).Day.ToString();
            ThangKTddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[5].Text)).Month.ToString();
            NamKTddl.SelectedValue = DateTime.Parse(HttpUtility.HtmlDecode(dong.Cells[5].Text)).Year.ToString();
        }
        protected void NgayBD_Load()
        {
            int i;
            for (i = 1990; i < 2030; i++)
            {
                NamBDddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 13; i++)
            {
                ThangBDddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 32; i++)
            {
                NgayBDddl.Items.Add(i.ToString());
            }
        }
        protected void NgayKT_Load()
        {
            int i;
            for (i = 1990; i < 2030; i++)
            {
                NamKTddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 13; i++)
            {
                ThangKTddl.Items.Add(i.ToString());
            }

            for (i = 1; i < 32; i++)
            {
                NgayKTddl.Items.Add(i.ToString());
            }
        }
    }
}